export const downloadImages = (canvas, images) => {
  return new Promise((resolve, reject) => {
    let len = images.length,  // 有几个url需要下载
        o = {},
        i = 0;

    function fn(data) {
      const img = canvas.createImage();
      img.src = data.url;
      img.onload = (e) => {
        o[data.name] = img;
        i++;
        if (i < len) {
          fn(images[i]);
        } else {
          resolve(o)
        }
      }
      img.onerror = (err) => {
        reject(err);
      }
    }

    fn(images[i]);
  });
};

export const clipArc = (context, left, top, width, height, radius, drawFn) => {
  context.save();
  context.beginPath();
  context.moveTo(left + radius, top);           // 创建开始点
  context.lineTo(left + width - radius, top);          // 创建水平线
  context.arcTo(left + width, top, left + width, top + radius, radius); // 创建弧
  context.lineTo(left + width, top + height - radius);         // 创建垂直线
  context.arcTo(left + width, top + height, left + width - radius, top + height, radius); // 创建弧
  context.lineTo(left + width - radius, top + height);         // 创建水平线
  context.arcTo(left, top + height, left, top + height - radius, radius); // 创建弧
  context.lineTo(left, top + radius);         // 创建垂直线
  context.arcTo(left, top, left + radius, top, radius); // 创建弧
  context.closePath();
  context.clip();
  if (typeof drawFn === 'function') {
    drawFn();
  }
  context.restore();
};
